<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------

/**
 * Created by PhpStorm.
 * Script Name: Follow.php
 * Create: 2020/7/24 下午9:21
 * Description: 粉丝
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mini\controller;

use think\Db;

class Follow extends Base
{
    /**
     * @var \app\common\model\MiniFollow
     */
    private $followM;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->followM = model('miniFollow');
    }

    /**
     * 首页
     * @return mixed
     * @throws \think\exception\DbException
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function index(){
        $where = ['mini_id' => $this->miniId];
        $search_key = input('search_key', '');
        $search_key && $where['nickname|mobile'] = ['like', '%'.$search_key.'%'];
        $data = $this->followM->page($this->pageSize, $where, ['update_time' => 'desc'], ['id','openid','nickname','headimgurl','mobile','create_time'], 1);
        $page = $data->appends(['search_key' => $search_key])->render();
        $assign = ['search_key' => $search_key, 'data' => $data, 'page' => $page];
        return $this->show($assign);
    }

    /**
     * 删除
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function delPost(){
        if(request()->isPost()){
            Db::startTrans();
            try {
                $post_data = input('post.');
                $map = ['mini_id' => $this->miniId, 'id' => $post_data['id']];
                $data = $this->followM->getOne($map);
                if(empty($data)){
                    $this->error('参数错误');
                }

                if($res = $this->followM->delOne($map)) {
                    $this->followM->getOneByMap(['mini_id' => $this->miniId, 'openid' => $data['openid']],
                        true, true
                    );
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $res = var_export($e->getMessage(), true);
            }
            if(is_string($res)) {
                $this->error('删除失败');
            }
            $this->success('操作成功');
        }
    }

    /**
     * 刷新
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function refreshPost(){
        if(request()->isPost()){
            $post_data = input('post.');
            $map = ['mini_id' => $this->miniId, 'id' => $post_data['id']];
            $data = $this->followM->getOne($map);
            if(empty($data)){
                $this->error('参数错误');
            }

            $this->followM->getOneByMap(['mini_id' => $this->miniId, 'openid' => $data['openid']],
                true, true
            );
            $this->success('操作成功');
        }
    }
}